<?php

namespace plugins\order\controller;



use cmf\controller\PluginAdminBaseController;

use think\Db;

use think\Request;

use app\common\lib\model_operate\Transit;



use plugins\order\model\OrderModel;

use plugins\finance\model\FinanceModel;

use plugins\pay\lib\Pay;

use plugins\order\model\OrderRefundModel;





class AuditOrderController extends PluginAdminBaseController

{



	//申请退款列表

	public function order_refund_list()

	{
		$param = $this->request->param();

		//流水记录

		$search =  Transit::createTags('search');

		$search->setParam($param);

		$table = Transit::createTags('table');

		$table->setParam($search);	

		$pagesize = isset( $param [ 'pagesize' ] ) ? $param [ 'pagesize' ] : 20;



		$sear = [];



		//权限检测 

		$check = ( new \plugins\store\Common )->getAllowAccess();

		if($check === true){

			// $where = ['store_id'=>'admin'];
			$store_id = 'admin';

		}else{

			// $where = ['store_id'=>$check];
			$store_id = $check;

		}		



        //流水记录列表

		$list = $table->query()

		        ->order('id desc')

		        ->where(['order_status'=>['in',['2','3']]])

		        ->where(['order_refund'=>['neq',4],'store_id'=>$store_id])

                ->paginates($pagesize,false,['query'=>$sear]);



		$this->assign('search',$search);

		$this->assign('table',$table);

		$this->assign('list',$list);



		return $this->fetch();		

	}



	//同意订单退款

	public function order_refund_agree()

	{

		$param = $this->request->param();



		//权限检测 

		$check = ( new \plugins\store\Common )->getAllowAccess();

		if($check === true){

			$check = 'admin';

			$finance_type = 'store';

		}else{

			$finance_type = 'store';

			$where['store_id'] = $check;

		}	



		$where['order_num'] = ['in',$param['order_num']];



		//所选内容

		$list = ( new OrderModel )

				->where($where)

				->select();



		//遍历结果

		foreach ($list as $key => $value) {

			if($value->order_refund != 2){

				continue;

			}



			//得到店铺的财务账户,类型为店铺store，绑定id为店铺id

			$finance = ( new FinanceModel )->get(['finance_type_id'=>$check,'finance_type'=>$finance_type]);



			if($value->order_status == 4){

				if($finance->balance < $value->order_money){

					return $this->error('订单'.$value->order_num.'操作失败,用户账户余额不足以退款！');

				}				

			}



			//调用pay创建退款

			$result = $value->orderReturn();



			if(!$result['status']){

				return $this->error($value->order_num.'操作失败,退款失败,'.$result['message']);

			}

		}

		return $this->success('操作成功,选中的待退款申请已通过');



	}



	//拒绝订单退款

	public function order_refund_refuse()

	{

		$param = $this->request->param();

		//权限检测 

		$check = ( new \plugins\store\Common )->getAllowAccess();

		if($check === true){

			

		}else{

			$where['store_id'] = $check;

		}	



		$where['order_num'] = ['in',$param['order_num']];



		//所选内容

		$list = ( new OrderModel )

				->where($where)

				->select();



		foreach ($list as $key => $value) {

			if($value->order_refund != 2){

				continue;

			}

			//得到用户的退款申请
			$refund = ( new OrderRefundModel )->get(['order_num'=>$value->order_num]);
			$refund->save(['store_remark'=>$param['reason'],'store_time'=>date('Y-m-d H:i:s')]);

			$value->order_refund = 3;

			$value->save();

		}



		return $this->success('操作成功,选中的订单退款申请已拒绝');				



	}



}